package com.iisigroup.http.test;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tw.gov.moi.ris.aw.webapply.doorplate.constants.SearchType;
import tw.gov.moi.ris.aw.webapply.doorplate.dto.DoorplateSearchDTO;

/**
 * 查詢中獎發票號碼清單
 * 
 * ***/
public class PB2CAPIVAN extends BaseCommonHttpMethod {
	private final static Logger LOGGER = LoggerFactory
			.getLogger(PB2CAPIVAN.class);

	public static void main(String[] args) throws Exception {

		new PB2CAPIVAN().testMethod();
	}

	public ResponseResult testMethod() throws Exception {
		final long times = 1l;
		final ResponseResult result = new ResponseResult();
		long totalTime = 0l;

		for (int c = 0; c < times; c++) {
			String url = "https://www.einvoice.nat.gov.tw/PB2CAPIVAN/invapp/InvApp";
			final long startTime = System.currentTimeMillis();
			final CloseableHttpClient httpclient = getHttpClient(null, 0);
			final HttpPost httpPost = new HttpPost(url);

			final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("version", "0.2"));
			nvps.add(new BasicNameValuePair("action", "QryWinningList"));
			nvps.add(new BasicNameValuePair("invTerm", "10106"));
//			nvps.add(new BasicNameValuePair("UUID", "10106"));
			nvps.add(new BasicNameValuePair("appID", "EINV8201411050701"));
			
			//必須指定參數為 UTF-8 編碼
			final Charset  charset =  Charset.forName("UTF-8");
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, charset));

			CloseableHttpResponse response2 = null;

			try {
				response2 = httpclient.execute(httpPost);
				final String responseStatus = response2.getStatusLine()
						.toString().intern();
				result.getListResult().add(responseStatus.toString().intern());
				// LOGGER.info(responseStatus);
				final HttpEntity entity2 = response2.getEntity();
				final String content = EntityUtils.toString(entity2);

				System.out.println(content);

				final Header[] allHeaders = response2.getAllHeaders();
				for (Header aHeader : allHeaders) {
					LOGGER.info("name:{} ,value:{}", aHeader.getName(),
							aHeader.getValue());
				}

				EntityUtils.consume(entity2);
				 
			} catch (final Exception e) {
				System.out.println("ERR:" + e.toString());
			} finally {
				if (response2 != null) {
					response2.close();
				}
			}
			final long intervalTime = System.currentTimeMillis() - startTime;
			totalTime += intervalTime;
			httpclient.close();
			LOGGER.info("{}", intervalTime);

		}
		result.setAverageTime(totalTime / times);

		// 如果要測試戶籍員的反應...則要把httpclient.close();註解掉,這樣才會使得stuck thread 不會消失
		return result;
	}

	public int getThreadNum() {
		return 0;
	}

}
